﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace VKCT
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct MOUSEINPUT
    {
         public int dx;
        public int dy;
        public int mouseData;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct KEYBDINPUT
    {
        public short wVk;
        public short wScan;
        public int dwFlags;
        public int time;
        public IntPtr dwExtraInfo;
    }

    [StructLayout(LayoutKind.Explicit)]
    internal struct Input
    {
        [FieldOffset(0)]
        public int type;
        [FieldOffset(4)]
        public MOUSEINPUT mi;
        [FieldOffset(4)]
        public KEYBDINPUT ki;
        [FieldOffset(4)]
        public HARDWAREINPUT hi;
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct HARDWAREINPUT
    {
        public int uMsg;
        public short wParamL;
        public short wParamH;
    }

    internal class INPUT
    {
        public const int MOUSE = 0;
        public const int KEYBOARD = 1;
        public const int HARDWARE = 2;
    }

    internal static class NativeMethods
    {
        [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        internal static extern IntPtr GetWindowLong(IntPtr hWnd, int nIndex);

        [DllImport("User32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        internal static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("User32.dll", EntryPoint = "SendInput", CharSet = CharSet.Auto)]
        internal static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, Int32 cbSize);

        [DllImport("Kernel32.dll", EntryPoint = "GetTickCount", CharSet = CharSet.Auto)]
        internal static extern int GetTickCount();

        [DllImport("User32.dll", EntryPoint = "GetKeyState", CharSet = CharSet.Auto)]
        internal static extern short GetKeyState(int nVirtKey);

        [DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Auto)]
        internal static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
    }
}

//class ScreenKeyboard
//{
//    public void Start(bool installMouseHook, bool installKeyboardHook) {
//     if (hMouseHook == IntPtr.Zero && installMouseHook) {
//         MouseHookProcedure = new HookProc(MouseHookProc);
//         hMouseHook = SetWindowsHookEx(
//             WH_MOUSE_LL,
//             MouseHookProcedure,
//             Marshal.GetHINSTANCE(
//             Assembly.GetExecutingAssembly().GetModules()[0]),
//             0
//       );

//        if (hMouseHook == IntPtr.Zero) {
//            int errorCode = Marshal.GetLastWin32Error();
//            Stop(true, false, false);

//            throw new Win32Exception(errorCode);
//        }
//    }

//    if (hKeyboardHook == IntPtr.Zero && installKeyboardHook) {            
//            KeyboardHookProcedure = new HookProc(KeyboardHookProc);
//       //install hook
//        hKeyboardHook = SetWindowsHookEx(
//            WH_KEYBOARD_LL,
//            KeyboardHookProcedure,
//            Marshal.GetHINSTANCE(
//            Assembly.GetExecutingAssembly().GetModules()[0]),
//            0);
//        // If SetWindowsHookEx fails.
//        if (hKeyboardHook == IntPtr.Zero) {
//            // Returns the error code returned by the last 
//            // unmanaged function called using platform invoke 
//            // that has the DllImportAttribute.SetLastError flag set. 
//            int errorCode = Marshal.GetLastWin32Error();
//            //do cleanup
//            Stop(false, true, false);
//            //Initializes and throws a new instance of the 
//            // Win32Exception class with the specified error. 
//            throw new Win32Exception(errorCode);
//        }
//    }
//}
//}